﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using BuddyTracker.Interfaces;
using BuddyTracker.Model;

namespace BuddyTracker.ViewModel.Encoders
{
    class AESLocationEncoder : ILocationEncoder
    {
        
        public static string _staticPass = "DADJJISJIDFJI##$2332jio3ioj3i2oriji32oir"; // >32
        public static string Masterpass = _staticPass;
        private TextLocationEncoder BasicTextEncoder;

        public AESLocationEncoder()
        {
            BasicTextEncoder = new TextLocationEncoder();
        }

        public string EncodeLocation(Geopoint location)
        {
            try
            {
                string text = BasicTextEncoder.EncodeLocation(location).Substring(10);
                return "FLWR|CRYPT|" + Encrypt(text);
            }
            catch
            {
                return null;
            }
            
        }

        public User DecodeString(string s)
        {
            try
            {
                if (s.StartsWith("FLWR|CRYPT|"))
                {
                    string text = "FLWR|TEXT|" + Decrypt(s.Substring(11));
                    return BasicTextEncoder.DecodeString(text);
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        public string Encrypt(string input)
        {
            // get IV, key and encrypt 
            var iv = CryptographicBuffer.CreateFromByteArray(
            Encoding.UTF8.GetBytes(Masterpass));
            var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
            var newPassword = Masterpass;
            // make sure we satify minimum length requirements 
            var buffer = CryptographicBuffer.ConvertStringToBinary(
              newPassword, BinaryStringEncoding.Utf8);
            var key = provider.CreateSymmetricKey(buffer);
            var encryptedBuffer = CryptographicEngine.Encrypt(
              key, CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8), iv);
            return CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
        }
        public string Decrypt(string input)
        {
            
            // get IV, key and decrypt 
            var iv = CryptographicBuffer.CreateFromByteArray(
            Encoding.UTF8.GetBytes(Masterpass));
            var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
            var newPassword = Masterpass;
            // make sure we satify minimum length requirements 
            var buffer = CryptographicBuffer.ConvertStringToBinary(
              newPassword, BinaryStringEncoding.Utf8);
            var key = provider.CreateSymmetricKey(buffer);
            var decryptedBuffer = CryptographicEngine.Decrypt(
              key, CryptographicBuffer.DecodeFromBase64String(input), iv);
            return CryptographicBuffer.ConvertBinaryToString(
             BinaryStringEncoding.Utf8, decryptedBuffer);
        }
       
        
        
    }
}

